// pages/questionnaire/questionnaire.js

import AreaList from '../../static/area.js'

var showPicker = false

    /**
     * 是否可提交
     */
var canCommit = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList:AreaList,
    user:wx.getStorageInfoSync('user'),
    telephone: wx.getStorageSync('telephone'),
    contacts: wx.getStorageSync('contacts'),
    contacts_telephone: wx.getStorageSync('contacts_telephone'),
    body:wx.getStorageSync('body'),
    istreatment:wx.getStorageSync('istreatment'),
    isolated:wx.getStorageSync('isolated'),
    inschool:wx.getStorageSync('inschool'),
    inguan:wx.getStorageSync('inguan'),
    location:wx.getStorageSync('location'),
    temperature:wx.getStorageSync('temperature'),
    /**
     * 控制弹出层是否弹出的值
     */
    show_body:false, 
    show_istreatment:false,
    show_isolated:false,
    show_inschool:false,
    show_inguan:false,
    show_location:false,
    /**
     * 选择器中的值
     */
    body_list: ['良好','异常（发热）','异常（咳嗽）','异常（头晕）','异常（腹泻）','异常（乏力）','异常（同时含两种以上异常状况）'],
    istreatment_list: ['否','是'],
    isolated_list: ['否','是'],
    inschool_list:['否','是'],
    inguan_list:['否','是'],
    /**
     * 是否已填报
     */
    isSubmit:false,
  },

  getIsSubmit: function(){
    wx.request({
      url: 'http://121.4.60.73:8080/wechat/judgeUserIsSubmit',
      method: 'GET',
      header: { 'content-type': 'application/json' },
      data: {
        'id': this.data.user.id,
      },
      success:(res) => {
        var resData = res.data;
        this.setData({
          isSubmit:resData
        })
      },
    })
  },

  judgeCommit: function(e){
    var that=this
    wx.setStorageSync('telephone', e.detail.value.telephone)
    wx.setStorageSync('contacts', e.detail.value.contacts)
    wx.setStorageSync('contacts_telephone', e.detail.value.contacts_telephone)
    wx.setStorageSync('body', that.data.body)
    wx.setStorageSync('istreatment', that.data.istreatment)
    wx.setStorageSync('isolated', that.data.isolated)
    wx.setStorageSync('temperature', e.detail.value.temperature)
    wx.setStorageSync('inschool', that.data.inschool)
    wx.setStorageSync('inguan', that.data.inguan)
    wx.setStorageSync('location', that.data.location)

    if(e.detail.value.telephone != "" && e.detail.value.contacts != "" &&
    e.detail.value.contacts_telephone != "" &&
    e.detail.value.temperature != "" && that.data.location != ""){
      canCommit = true
    }
  },

  submit: function(e){
    var that = this;
    this.judgeCommit(e)
    if(canCommit == false){
      wx.showToast({
        title: '还有未填项',
        image: '/images/no.png',
        duration: 2000
      })
    }
    else{
      console.log('提交')
      wx.request({
        url: 'http://121.4.60.73:8080/wechat/insertDailyData',
        method: 'GET',
        header: { 'content-type': 'application/json' },
        data: {
          'sno': that.data.user.id,
          'telephone': e.detail.value.telephone,
          'contacts':e.detail.value.contacts,
          'contacts_telephone': e.detail.value.contacts_telephone,
          'body': that.data.body,
          'istreatment':that.data.istreatment,
          'isolated':that.data.isolated,
          'inschool':that.data.inschool,
          'inguan':that.data.inguan,
          'location':that.data.location,
          'temperature': e.detail.value.temperature,
        },
        success: function (res) {
          that.setData({
            isSubmit:true,
          })
        }
      }) 
    }
  },

  ToSetStorageSync:function(){
    if(wx.getStorageSync('body') == ""){
      this.setData({
        body:0
      })
    }
    if(wx.getStorageSync('istreatment') == ""){
      this.setData({
        istreatment:0
      })
    }
    if(wx.getStorageSync('isolated') == ""){
      this.setData({
        isolated:0
      })
    }
    if(wx.getStorageSync('inschool') == ""){
      this.setData({
        inschool:0
      })
    }
    if(wx.getStorageSync('inguan') == ""){
      this.setData({
        inguan:0
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this
    var user = JSON.parse(options.user);
    self.setData({
      user: user
    })
    self.getIsSubmit()
    self.ToSetStorageSync()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
   /**
   * 身体状况相关函数
   */
  show_bodyPopup(e){      //点击选择，打开弹出层（选择器）
    this.setData({show_body:true})
  },
  bodyOnClose() {     //点击空白处开闭弹出层（选择器）及选择器左上角的取消
    this.setData({ show_body: false });
  },
  bodyOnConfirm(e){    //选择器右上角的确定，点击确定获取值
    this.setData({
      body:e.detail.index,
      show_body:false
    })
  },

  /**
   * 是否就医相关函数
   */
  show_istreatmentPopup(e){      //点击选择，打开弹出层（选择器）
    this.setData({show_istreatment:true})
  },
  istreatmentOnClose() {     //点击空白处开闭弹出层（选择器）及选择器左上角的取消
    this.setData({ show_istreatment: false });
  },
  istreatmentOnConfirm(e){    //选择器右上角的确定，点击确定获取值
    this.setData({
      istreatment:e.detail.index,
      show_istreatment:false
    })
  },

  /**
   * 是否隔离相关函数
   */
  show_isolatedPopup(e){      //点击选择，打开弹出层（选择器）
    this.setData({show_isolated:true})
  },
  isolatedOnClose() {     //点击空白处开闭弹出层（选择器）及选择器左上角的取消
    this.setData({ show_isolated: false });
  },
  isolatedOnConfirm(e){    //选择器右上角的确定，点击确定获取值
    this.setData({
      isolated:e.detail.index,
      show_isolated:false
    })
  },

  /**
   * 是否在校相关函数
   */
  show_inschoolPopup(e){      //点击选择，打开弹出层（选择器）
    this.setData({show_inschool:true})
  },
  inschoolOnClose() {     //点击空白处开闭弹出层（选择器）及选择器左上角的取消
    this.setData({ show_inschool: false });
  },
  inschoolOnConfirm(e){    //选择器右上角的确定，点击确定获取值
    if(e.detail.index == 1){
      this.setData({
        inguan:1,
        inschool:e.detail.index,
        show_inschool:false
      })
    }
    else{
      this.setData({
        inschool:e.detail.index,
        show_inschool:false
      })
    }
    
  },

  /**
   * 是否在莞相关函数
   */
  show_inguanPopup(e){      //点击选择，打开弹出层（选择器）
    this.setData({show_inguan:true})
  },
  inguanOnClose() {     //点击空白处开闭弹出层（选择器）及选择器左上角的取消
    this.setData({ show_inguan: false });
  },
  inguanOnConfirm(e){    //选择器右上角的确定，点击确定获取值
    this.setData({
      inguan:e.detail.index,
      show_inguan:false
    })
  },
/**
   * 地址相关函数
   */
  show_locationPopup(e){      //点击选择，打开弹出层（选择器）
    this.setData({show_location:true})
  },
  locationOnClose() {     //点击空白处开闭弹出层（选择器）及选择器左上角的取消
    this.setData({ show_location: false });
  },
  locationOnConfirm(e){    //选择器右上角的确定，点击确定获取值
    console.log(e)
    this.setData({
      location:e.detail.values[0].name+e.detail.values[1].name+e.detail.values[2].name,
      show_location:false
    })
  },

})